﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SimplifyLauncher
{
    public partial class SettingsForm : Form
    {
        Launcher launcher;
        List<Rectangle> previewLocations;
        Rectangle previewOutline;
        Pen pen, penDotted;
        Panel tabPanel, previewPanel;
        TabControl tabControl;
        TabPage tabGeneral, tabAppearance, tabAbout;
        TextBox iconWidthBox, iconHeightBox, rowsBox, columnsBox;

        int screenWidth, screenHeight;
        bool drawLocations;

        const int PREVIEW = 4;
        const int PADDING = 10;
        const int BOX_WIDTH = 75;
        const int MIDDLE_LEFT = PADDING * 4 + BOX_WIDTH;

        public SettingsForm(Launcher launcher)
        {
            this.BackColor = Color.FromArgb(25, 25, 25);
            this.FormBorderStyle = FormBorderStyle.None;
            //this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.Text = "Settings";

            FontFamily calibri = new FontFamily("Calibri");
            this.Font = new Font(calibri, 9, FontStyle.Regular);

            // Pens
            pen = new Pen(SystemColors.ControlDark, 1);
            float[] dashes = { 1, 1 };
            penDotted = new Pen(Color.White, 1);
            penDotted.DashPattern = dashes;

            screenWidth = Screen.PrimaryScreen.Bounds.Width;
            screenHeight = Screen.PrimaryScreen.Bounds.Height;

            tabPanel = new Panel();
            tabControl = new TabControl();
            tabGeneral = new TabPage();
            tabAppearance = new TabPage();
            tabAbout = new TabPage();

            this.Controls.Add(tabPanel);
            tabPanel.Controls.Add(tabControl);

            // ***GENERAL TAB *** //

            tabGeneral.Text = "General";
            tabGeneral.BackColor = Color.FromArgb(25, 25, 25);
            tabControl.Controls.Add(tabGeneral);

            // *** APPEARANCE TAB *** //

            tabAppearance.Text = "Appearance";
            tabControl.Controls.Add(tabAppearance);

            Label iconWidthLabel = new Label() { Left = PADDING, Top = PADDING, AutoSize = true, Text = "Icon Width" };
            iconWidthBox = new TextBox() { Left = PADDING, Top = iconWidthLabel.Bottom, Width = BOX_WIDTH };
            iconWidthBox.Text = launcher.IconWidth.ToString();
            iconWidthBox.TextChanged += new EventHandler(Appearance_Changed);
            tabAppearance.Controls.Add(iconWidthLabel);
            tabAppearance.Controls.Add(iconWidthBox);

            Label iconHeightLabel = new Label() { Left = PADDING, Top = iconWidthBox.Bottom + PADDING, AutoSize = true, Text = "Icon Height" };
            iconHeightBox = new TextBox() { Left = PADDING, Top = iconHeightLabel.Bottom, Width = BOX_WIDTH };
            iconHeightBox.Text = launcher.IconHeight.ToString();
            iconHeightBox.TextChanged += new EventHandler(Appearance_Changed);
            tabAppearance.Controls.Add(iconHeightLabel);
            tabAppearance.Controls.Add(iconHeightBox);

            Label rowsLabel = new Label() { Left = PADDING, Top = iconHeightBox.Bottom + PADDING, AutoSize = true, Text = "Rows" };
            rowsBox = new TextBox() { Left = PADDING, Top = rowsLabel.Bottom, Width = BOX_WIDTH };
            rowsBox.Text = launcher.Rows.ToString();
            rowsBox.TextChanged += new EventHandler(Appearance_Changed);
            tabAppearance.Controls.Add(rowsLabel);
            tabAppearance.Controls.Add(rowsBox);

            Label columnsLabel = new Label() { Left = PADDING, Top = rowsBox.Bottom + PADDING, AutoSize = true, Text = "Columns" };
            columnsBox = new TextBox() { Left = PADDING, Top = columnsLabel.Bottom, Width = BOX_WIDTH };
            columnsBox.Text = launcher.Columns.ToString();
            columnsBox.TextChanged += new EventHandler(Appearance_Changed);
            tabAppearance.Controls.Add(columnsLabel);
            tabAppearance.Controls.Add(columnsBox);

            Label previewLabel = new Label() { Left = MIDDLE_LEFT, Top = PADDING, AutoSize = true, Text = "Preview" };
            tabAppearance.Controls.Add(previewLabel);

            previewPanel = new Panel() { Left = MIDDLE_LEFT, Top = previewLabel.Bottom + PADDING, Width = screenWidth / PREVIEW + 4, Height = screenHeight / PREVIEW + 4, BackColor = Color.Black };
            tabAppearance.Controls.Add(previewPanel);

            GeneratePreviewLocationsAndBackground();
            previewPanel.Paint += new PaintEventHandler(Preview_Paint);

            // *** ABOUT TAB *** //

            tabAbout.Text = "About";
            tabControl.Controls.Add(tabAbout);

            // *** WINDOW SIZING *** //

            tabControl.ItemSize = new Size(0, PADDING * 2);
            tabControl.Size = new Size(previewPanel.Right + PADDING * 2, previewPanel.Bottom + tabControl.ItemSize.Height + PADDING * 2);
            tabPanel.Size = new Size(tabControl.Width + PADDING, tabControl.Height + PADDING);
            tabControl.Left = PADDING / 2 + 2;
            tabControl.Top = PADDING / 2 + 2;
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        }

        public void OpenTab(String tab)
        {
            switch (tab)
            {
                case "general":
                    tabControl.SelectedTab = tabGeneral;
                    break;
                case "appearance":
                    tabControl.SelectedTab = tabAppearance;
                    break;
            }
            this.ShowDialog();
        }

        private int TextBoxToInt(TextBox box)
        {
            int boxAsInt;
            box.ForeColor = Color.Black;

            if (!int.TryParse(box.Text, out boxAsInt))
            {
                drawLocations = false;
                box.ForeColor = Color.Red;
                boxAsInt = 0;
            }

            return boxAsInt;
        }

        private void GeneratePreviewLocationsAndBackground()
        {
            drawLocations = true;

            int iconWidth = TextBoxToInt(iconWidthBox);
            int iconHeight = TextBoxToInt(iconHeightBox);
            int rows = TextBoxToInt(rowsBox);
            int columns = TextBoxToInt(columnsBox);

            if (!drawLocations)
                return;

            // Calculate gap sizes (distance between shortcuts and screen edges)
            int gapX = (screenWidth - (iconWidth * columns)) / (columns + 1);
            int gapY = (screenHeight - (iconHeight * rows)) / (rows + 1);

            // Initialize all other variables with empty lists and zeros
            previewLocations = new List<Rectangle>();
            int x = 0;
            int y = 0;

            // Create all preview location rectangles
            for (int i = 0; i < columns; i++)
            {
                x += gapX;
                y = 0;
                for (int j = 0; j < rows; j++)
                {
                    y += gapY;

                    // Create preview location rectangle and add to 'previewLocations'
                    Rectangle location = new Rectangle(x / PREVIEW + 1, y / PREVIEW + 1, iconWidth / PREVIEW, iconHeight / PREVIEW);
                    previewLocations.Add(location);

                    y += iconHeight;
                }
                x += iconWidth;
            }

            // Create preview outline rectangle
            previewOutline = new Rectangle(0, 0, screenWidth / PREVIEW + 3, screenHeight / PREVIEW + 3);
        }

        private void Preview_Paint(object sender, PaintEventArgs e)
        {
            // Preview outline
            e.Graphics.DrawRectangle(pen, previewOutline);

            // All preview locations
            if (drawLocations)
                e.Graphics.DrawRectangles(penDotted, previewLocations.ToArray());
        }

        private void Appearance_Changed(object sender, EventArgs e)
        {
            GeneratePreviewLocationsAndBackground();
            this.Refresh();
        }
    }
}
